import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.15
import QtQuick.Dialogs 1.3

import "./common/"

Item {
    id: sceneControl
    anchors.fill: parent

    Rectangle{
        anchors.horizontalCenter: parent.horizontalCenter
        width: 10
        height: 10
        color: "red"
    }

    property string codeTxt: ""

//    property var meshs: new Array

    property var meshpaths: undefined

    MyObj{
        id: myObj
    }

    // 创建对象
    function clickType(name)
    {
        console.log("clickType: ", name)
        if(name === "...") return

//        var objData = Qt.createComponent("common/MyObj.qml")
        var objData
        if(name === "camera")
        {
            objData = myObj.initCamera(name)
        }
        else if(name === "directionalLight")
        {
            objData = myObj.initDirectionLight(name)
        }
        else if(name === "icon"){
            objData = myObj.initIcon(name)
        }
        else{
            objData = myObj.init(name)
        }

        listModel2.append({ "name": name, "objData": objData })
    }

    // 选择对象
    function selectNode(data)
    {
        console.log("selectNode: ", data.name)
        console.log("name: ", data.name)

        console.log("posX: ", data.posX)
        console.log("posY: ", data.posY)
        console.log("posZ: ", data.posZ)

        // 更新属性值
        itemPosX.tf.text = data.posX
        itemPosY.tf.text = data.posY
        itemPosZ.tf.text = data.posZ

        itemRotationX.tf.text = data.rotationX
        itemRotationY.tf.text = data.rotationY
        itemRotationZ.tf.text = data.rotationZ

        itemScaleX.tf.text = data.scaleX
        itemScaleY.tf.text = data.scaleY
        itemScaleZ.tf.text = data.scaleZ

        tfcolor.text = data.color

        if(data.name === "directionalLight")
        {
            itemLightColor.tf.text = data.color
            itemLightAmbientColor.tf.text = data.ambientColor
        }

        setCode(data)
    }

    function delNode(name, index)
    {
        console.log("delNode: ", name)
//        if(name === "camera")
//        {
//            // 不能删除camera
//            messageDialog.text = "不能删除camera！"
//            messageDialog.visible = true
//            return
//        }
        messageDialog.text = "不能删除" + name
        messageDialog.visible = true
        return

//        listModel2.remove(index)
    }

    function setPropertyValue(objData)
    {
        console.log("setPropertyValue: ", objData.name)
        if(objData.name === "camera")
        {
            camera.x = objData.posX
            camera.y = objData.posY
            camera.z = objData.posZ

            camera.eulerRotation.x = objData.rotationX
            camera.eulerRotation.y = objData.rotationY
            camera.eulerRotation.z = objData.rotationZ

            camera.scale.x = objData.scaleX
            camera.scale.y = objData.scaleY
            camera.scale.z = objData.scaleZ
        }
        else if(objData.name === "directionalLight")
        {
            light.x = objData.posX
            light.y = objData.posY
            light.z = objData.posZ

            light.eulerRotation.x = objData.rotationX
            light.eulerRotation.y = objData.rotationY
            light.eulerRotation.z = objData.rotationZ

            light.color = objData.color
            light.ambientColor = objData.ambientColor
            light.brightness = objData.brightness
        }
        else if(objData.name === "icon")
        {
            var obj = modelIcon

            obj.x = objData.posX
            obj.y = objData.posY
            obj.z = objData.posZ

            obj.eulerRotation.x = objData.rotationX
            obj.eulerRotation.y = objData.rotationY
            obj.eulerRotation.z = objData.rotationZ

            obj.scale.x = objData.scaleX
            obj.scale.y = objData.scaleY
            obj.scale.z = objData.scaleZ
        }
        else if(objData.name === "node")
        {
            var obj = view3D.customNode

            obj.posX = objData.posX
            obj.posY = objData.posY
            obj.posZ = objData.posZ

            obj.rotationX = objData.rotationX
            obj.rotationY = objData.rotationY
            obj.rotationZ = objData.rotationZ

            obj.scaleX = objData.scaleX
            obj.scaleY = objData.scaleY
            obj.scaleZ = objData.scaleZ

            obj.diffuseColor = objData.color
        }

        setCode(objData)

        listModel2.set(view2.currentIndex, { "name": objData.name, "objData": objData })
    }

    function setCode(objData)
    {
        if(objData.name === "camera")
        {
            codeTxt = "PerspectiveCamera {\n  id: camera\n  x: " + objData.posX +
                    "\n  y: " + objData.posY +
                    "\n  z: " + objData.posZ +
                    "\n  eulerRotation.x: " + objData.rotationX +
                    "\n  eulerRotation.y: " + objData.rotationY +
                    "\n  eulerRotation.z: " + objData.rotationZ +
                    "\n  scale.x: " + objData.scaleX +
                    "\n  scale.y: " + objData.scaleY +
                    "\n  scale.z: " + objData.scaleZ +
                    "\n  fieldOfView: 60\n  fieldOfViewOrientation: PerspectiveCamera.Vertical\n}"
        }
        else if(objData.name === "directionalLight")
        {
            codeTxt = "DirectionalLight {\n  id: directionalLight\n  x: " + objData.posX +
                    "\n  y: " + objData.posY +
                    "\n  z: " + objData.posZ +
                    "\n  eulerRotation.x: " + objData.rotationX +
                    "\n  eulerRotation.y: " + objData.rotationY +
                    "\n  eulerRotation.z: " + objData.rotationZ +
                    "\n  color: \"" + objData.color + "\"" +
                    "\n  ambientColor: \"" + objData.ambientColor + "\"" +
                    "\n  brightness: " + objData.brightness +
                    "\n}"
        }
        else if(objData.name === "node")
        {
            codeTxt = "CustomNode{\n  id: customNode\n  x: " + objData.posX +
                    "\n  y: " + objData.posY +
                    "\n  z: " + objData.posZ +
                    "\n  rotationX: " + objData.rotationX +
                    "\n  rotationY: " + objData.rotationY +
                    "\n  rotationZ: " + objData.rotationZ +
                    "\n  scaleX: " + objData.scaleX +
                    "\n  scaleY: " + objData.scaleY +
                    "\n  scaleZ: " + objData.scaleZ +
                    "\n  diffuseColor: \"" + objData.color + "\"" +
                    "\n}"
        }
        else if(objData.name === "icon")
        {
            codeTxt = "Model{\n  id: modelIcon\n  x: " + objData.posX +
                    "\n  y: " + objData.posY +
                    "\n  z: " + objData.posZ +
                    "\n  eulerRotation.x: " + objData.rotationX +
                    "\n  eulerRotation.y: " + objData.rotationY +
                    "\n  eulerRotation.z: " + objData.rotationZ +
                    "\n  scale.x: " + objData.scaleX +
                    "\n  scale.y: " + objData.scaleY +
                    "\n  scale.z: " + objData.scaleZ +
                    "\n}"
        }
    }

    Component.onCompleted: {
        clickType("camera")
        if(listModel2.count > 0)
        {
            view2.currentIndex = 0
            var d = listModel2.get(view2.currentIndex).objData
            selectNode(d)
            setPropertyValue(d)
        }

        clickType("directionalLight")
        if(listModel2.count > 0)
        {
            view2.currentIndex++
            var d = listModel2.get(view2.currentIndex).objData
            selectNode(d)
            setPropertyValue(d)
        }

        clickType("icon")
        if(listModel2.count > 0)
        {
            view2.currentIndex++
            var d = listModel2.get(view2.currentIndex).objData
            selectNode(d)
            setPropertyValue(d)
        }

        meshpaths = Mytool.getMeshs()
        for(var i in meshpaths)
        {
            cbItems.append({ "name": meshpaths[i] })
        }

        clickType("node")
        if(listModel2.count > 0)
        {
            view2.currentIndex++
            var d = listModel2.get(view2.currentIndex).objData
            selectNode(d)
            setPropertyValue(d)
        }
    }

    ListModel{
        id: listModel
        ListElement {
            name: "node"
        }
        ListElement {
            name: "..."
        }
    }

    ListModel{
        id: listModel2
    }

    ListModel {
        id: cbItems
    }

    Item{
        id: leftItem1
        x: 5
        width: 200
        height: 122

        Column{
            BaseRect{
                width: 200; height: 22
                Label {
                    anchors.centerIn: parent
                    text: "类型"
                    color: "white"
                }
            }

            BaseRect{
                width: 200; height: 100
                color: "white"
                ListView {
                    anchors.fill: parent
                    spacing: 5

                    model: listModel
                    delegate: Button {
                        width: 200
                        text: name
                        onClicked: {

//                            clickType(text)
                            messageDialog.text = "不支持动态添加"
                            messageDialog.visible = true
                        }
                    }
                }
            }
        }
    }

    Item{
        id: leftItem2
        x: 5
        width: 200
        height: 222
        anchors.top: leftItem1.bottom

        Column{
            BaseRect{
                width: 200; height: 22
                Label {
                    anchors.centerIn: parent
                    text: "场景"
                    color: "white"
                }
            }

            BaseRect{
                width: 200; height: 200
                color: "white"
                ListView {
                    id: view2
                    anchors.fill: parent
                    spacing: 0
                    clip: true

                    model: listModel2
                    delegate: BaseRect{
                        id: lbr
                        width: 200; height: 40
                        color: isSelect ? Qt.rgba(49/255, 78/255, 120/255, 1) : "white"
                        property bool isSelect: view2.currentIndex == index
                        Row{
                            spacing: 1
                            anchors.verticalCenter: parent.verticalCenter
                            height: tf.height

                            TextField{
                                id: tf
                                text: name
                                width: 137
                                readOnly: true
                            }
                            Button {
                                text: "选择"
                                width: 40
                                onClicked: {
                                    view2.currentIndex = index
                                    var objData = listModel2.get(view2.currentIndex).objData
                                    selectNode(objData)
                                }
                            }
                            Button {
                                text: "X"
                                width: 20
                                onClicked: delNode(tf.text, index)
                            }
                        }
                    }
                }
            }
        }
    }

    Button{
        x: 5
        anchors.top: leftItem2.bottom
        text: "隐藏场景 F11"
        onClicked: {
            myKeyword.focus = true
            sceneControl.visible = !sceneControl.visible
        }
    }


    Item{
        id: rightItem1
        anchors.right: parent.right
        width: 300
        height: parent.height

        TextArea{
            id: tx
            anchors.fill: parent
            text: codeTxt
            selectByKeyboard: true
            selectByMouse: true
        }
    }

    Item{
        id: rightItem2
        anchors.right: rightItem1.left
        width: 200
        height: 400

        Column{
            BaseRect{
                width: rightItem2.width; height: 22
                Label {
                    anchors.centerIn: parent
                    text: "位置"
                    color: "white"
                }
            }
            GroupBox{
                width: rightItem2.width;
                height: c1.height+20

                Column{
                    id: c1
                    spacing: 5
                    width: parent.width
                    height: itemPosX.height*3+10

                    MyCustomLine{
                        id: itemPosX
                        width: parent.width
                        labelTxt: "X: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.posX = v
                            setPropertyValue(objData)
                        }
                    }
                    MyCustomLine{
                        id: itemPosY
                        width: parent.width
                        labelTxt: "Y: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.posY = v
                            setPropertyValue(objData)
                        }
                    }
                    MyCustomLine{
                        id: itemPosZ
                        width: parent.width
                        labelTxt: "Z: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.posZ = v
                            setPropertyValue(objData)
                        }
                    }
                }
            }

            BaseRect{
                width: rightItem2.width; height: 22
                Label {
                    anchors.centerIn: parent
                    text: "旋转"
                    color: "white"
                }
            }
            GroupBox{
                width: rightItem2.width;
                height: c2.height+20

                Column{
                    id: c2
                    spacing: 5
                    width: parent.width
                    height: itemRotationX.height*3+10

                    MyCustomLine{
                        id: itemRotationX
                        width: parent.width
                        labelTxt: "X: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.rotationX = v
                            setPropertyValue(objData)
                        }
                    }
                    MyCustomLine{
                        id: itemRotationY
                        width: parent.width
                        labelTxt: "Y: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.rotationY = v
                            setPropertyValue(objData)
                        }
                    }
                    MyCustomLine{
                        id: itemRotationZ
                        width: parent.width
                        labelTxt: "Z: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.rotationZ = v
                            setPropertyValue(objData)
                        }
                    }
                }
            }

            BaseRect{
                width: rightItem2.width; height: 22
                Label {
                    anchors.centerIn: parent
                    text: "缩放"
                    color: "white"
                }
            }
            GroupBox{
                width: rightItem2.width;
                height: c3.height+20

                Column{
                    id: c3
                    spacing: 5
                    width: parent.width
                    height: itemScaleX.height*3+10

                    MyCustomLine{
                        id: itemScaleX
                        width: parent.width
                        labelTxt: "X: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.scaleX = v
                            setPropertyValue(objData)
                        }
                    }
                    MyCustomLine{
                        id: itemScaleY
                        width: parent.width
                        labelTxt: "Y: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.scaleY = v
                            setPropertyValue(objData)
                        }
                    }
                    MyCustomLine{
                        id: itemScaleZ
                        width: parent.width
                        labelTxt: "Z: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.scaleZ = v
                            setPropertyValue(objData)
                        }
                    }
                }
            }

            BaseRect{
                width: rightItem2.width; height: 22
                visible: listModel2.get(view2.currentIndex).objData.name !== "camera"
                Label {
                    anchors.centerIn: parent
                    text: "模型"
                    color: "white"
                }
            }
            GroupBox{
                width: rightItem2.width;
                height: c4.height+20
                visible: listModel2.get(view2.currentIndex).objData.name !== "camera"

                ComboBox {
                    id: c4
                     model: cbItems
                     width: parent.width
                     onCurrentIndexChanged: {
                         console.debug(cbItems.get(currentIndex).name, view2.currentIndex)
//                         var node = meshs[view2.currentIndex-1]
                         var node = view3D.customNode
                         node.src = "file:///" +cbItems.get(currentIndex).name
                     }
                 }

            }

            BaseRect{
                id: brc
                width: rightItem2.width; height: 22
                visible: listModel2.get(view2.currentIndex).objData.name === "node" || listModel2.get(view2.currentIndex).objData.name === "icon"
                Label {
                    anchors.centerIn: parent
                    text: "颜色"
                    color: "white"
                }
            }
            GroupBox{
                width: rightItem2.width;
                height: rc1.height+20
                visible: brc.visible

                Row{
                    id: rc1
                    width: parent.width
                    spacing: 1
                    height: tfcolor.height

                    TextField{
                        id: tfcolor
                        text: colorDialog.color
                        onTextChanged: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.color = tfcolor.text
                            setPropertyValue(objData)
                        }
                    }

                    Button{
                        text: "选色盘"
                        onClicked: {
                            colorDialog.visible = true
                        }
                    }
                }
            }

            BaseRect{
                width: rightItem2.width; height: 22
                visible: listModel2.get(view2.currentIndex).objData.name === "directionalLight"
                Label {
                    anchors.centerIn: parent
                    text: "灯光"
                    color: "white"
                }
            }
            GroupBox{
                width: rightItem2.width;
                height: c5.height+20
                visible: listModel2.get(view2.currentIndex).objData.name === "directionalLight"

                Column{
                    id: c5
                    spacing: 5
                    width: parent.width
                    height: itemLightColor.height*3+10

                    MyCustomLine2{
                        id: itemLightColor
                        width: parent.width
                        labelTxt: "颜色: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.color = v

                            setPropertyValue(objData)
                        }
                    }
                    MyCustomLine2{
                        id: itemLightAmbientColor
                        width: parent.width
                        labelTxt: "环境光: "
                        onSetValue: {
                            if(view2.currentIndex < 0) return
                            var objData = listModel2.get(view2.currentIndex).objData
                            objData.ambientColor = v
                            setPropertyValue(objData)
                        }
                    }
                }

            }

        }
    }


    MessageDialog {
         id: messageDialog
         title: "提示"
         text: "未赋值"
         visible: false
         icon: StandardIcon.Warning
     }

    ColorDialog{
        id: colorDialog
        visible: false
        onAccepted: {
            tfcolor.text = colorDialog.color
        }
    }
}
